package co.fitstart.admin.web.controller.courseware;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.FitnessTag;


/**
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/fitness-tags")
@MenuSetting("menu-course")
public class FitnessTagController {
    
    private static final Logger logger = LoggerFactory.getLogger(FitnessTagController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<FitnessTag> fitnessTagService;
    
    /**
     * list the FitnessTag
     * 
     * @param page
     * @return
     */
    @RequestMapping( value="" , method = RequestMethod.GET)
    @MenuSetting("submenu-fitness-tag")
    public ModelAndView list(Page page, HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-tag/fitness-tag-list");
        
        List<FitnessTag> fitnessTags =fitnessTagService.listByPage(FitnessTag.class, page);
        
        modelAndView.addObject("fitnessTags" ,fitnessTags);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping( value="/create" , method = RequestMethod.GET)
    public ModelAndView create(){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-tag/fitness-tag-create");
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param fitnessTag
     * @return
     */
    @RequestMapping( value="" , method = RequestMethod.POST)
    public @ResponseBody String onCreate(FitnessTag fitnessTag){
        
        Result result = new Result();
        
        try{
            fitnessTagService.save(fitnessTag.initialize());
            
            result.success().message("新添标签成功！");
        }catch (Exception e) {
            
            logger.error("#onCreate() - ", e);
            
            result.fail().message("新添标签失败！");
        }
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param tagId
     * @return
     */
    @RequestMapping( value="/{tagId}/edit" , method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("tagId") Long tagId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-tag/fitness-tag-edit");
        
        FitnessTag fitnessTag = fitnessTagService.load(FitnessTag.class, tagId);
        
        modelAndView.addObject("fitnessTag", fitnessTag);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param fitnessTag
     * @return
     */
    @RequestMapping( value="/{tagId}" , method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("tagId")FitnessTag fitnessTag){
        
        Result result = new Result();
        
        try{
            fitnessTagService.update(fitnessTag.edit());
            
            result.success().message("修改标签成功！");
        }catch (Exception e) {
            
            logger.error("#onEdit() - ", e);
            
            result.fail().message("修改标签失败！");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param tagId
     * @return
     */
    @RequestMapping( value="/{tagId}/delete" , method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("tagId") Long tagId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-tag/fitness-tag-delete");
        
        modelAndView.addObject("tagId", tagId);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param tagId
     * @return
     */
    @RequestMapping( value="/{tagId}" , method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@RequestParam("tagId") Long tagId ){
        
        Result result = new Result();
        
        try{
            FitnessTag fitnessTag =fitnessTagService.load(FitnessTag.class, tagId);
            
            fitnessTagService.update(fitnessTag.disable());
            
            result.success().message("删除标签成功！");
        }catch (Exception e) {
            
            logger.error("#onDelete() - ", e);
            
            result.fail().message("删除标签失败！");
        }
        return JsonUtils.toJsonString(result);
    }
}
